<?php
html::printPath(array(
		'seminar.form-title' => 'admin/seminar/all',
		$seminar['name'] => '/admin/seminar/display?id=' . $seminar['id'],
		'seminar.karmayogaAssignment' => ''
));
$msg->printMessages();

$fb = new FormBuilder('karmayogaAssignment', $settingsFormData);
$fb->setAction('/admin/karmayogaAssignment/saveSettings');
$fb->selectMany('batch_codes', $batchOptions);
$fb->submitButton();
$fb->write();

function writeTable($tableUuid, $registrationList, $karmayogaTypeOptions) {
	if ($registrationList) {
		$elb = new EntityListBuilder('registration', $registrationList);
		$elb->setUuid($tableUuid);
		$elb->checkbox('registration_ids[]', 'id')->noLink();
		$elb->integer('id')->linkWithValue('/admin/registration/display?id=', 'id');
		$elb->text('person_id')->doubleValue('last_name', 'first_name')->alignLeft()->orderByField('last_name')->linkWithValue('/admin/person/display?id=', 'person_id');
		$elb->date('birth_date')->labelKey('person.birth_date');
		$elb->text('country_name')->labelKey('person.country_of_residence_code');
		// $elb->timestamp('created_at');
		// $elb->longText('payment_organization_name')->labelKey('registration.payment_organization_id');
		$elb->text('batch_code')->valueFunction('batchValue');
		$elb->longText('org_notes');
		$elb->longText('program_notes');
		$elb->text('preferred_karmayoga_type_name')->labelKey('preferred_karmayoga_type_code');
		$elb->combobox('assigned_karmayoga_type_code', $karmayogaTypeOptions, 'onchange="assignKarmayoga(this)"')->addNameSuffix('id');
		$elb->write();
	}
}

function batchValue($registration, $entityListField) {
	return format::dateInterval($registration['begin_date'], $registration['end_date']) . ' (' . $registration['batch_code'] . ')';
}

echo form::open('/admin/karmayogaAssignment/assignSelected');

echo '<h3>' . text::getEscaped('karmayogaAssignment.unassigned') . ' <span style="font-weight:normal">(' . count($registrationList) . ')</span></h3>';
if ($registrationList) {
	writeTable($this->REGISTRATION_LIST_UUID, $registrationList, $karmayogaTypeOptions);
}
else {
	// TODO: there is no unassigned karmayoga registration
}

if ($usedKarmayogaTypeList) {
	foreach ($usedKarmayogaTypeList as $karmayogaType) {
		echo '<h3>' . $karmayogaType['name'] . ' <span style="font-weight:normal">(' . count($karmayogaType['registrationList']) . ')</span>' . '</h3>';
		writeTable($this->REGISTRATION_LIST_UUID . '-' . $karmayogaType['code'], $karmayogaType['registrationList'], $karmayogaTypeOptions);
	}
}

echo '<p style="padding: 1em 0 0 1em">' .
		text::get('karmayogaAssignment-assignSelected') . ': ' . form::comboBox('karmayoga_type_code', $karmayogaTypeOptions, array()) .
		form::submitButton('save') .
		'</p>';

echo form::close();
?>

<script type="text/javascript">
function assignKarmayoga(combobox) {
	var $checkbox = $(combobox).closest('tr').find('input[name="registration_ids[]"]');
	var registrationId = $checkbox.attr('value');
	var karmayogaTypeCode = combobox.value;
        document.location.href = 'assignOne?registration_id=' + registrationId + '&karmayoga_type_code=' + karmayogaTypeCode;
    }
</script>
